home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0095_Inline String Routines.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  8KB  |  202 lines

  1. {
  2. How do I make from a procedure or function an inline version?
  3. If I run the following program, the computer locks up. What's wrong??
  4. Help!!
  5. }
  6. var
  7.   s1, s2: string;
  8.  
  9. procedure CopySubStr( Str1: string; start, nrchars: byte; var Str2: string );
  10.   { copy part of Str1 (beginning at start for nrchars) to Str2
  11.     if start > length of Str1, Str2 will contain a empty string.
  12.     if nrchars specifies more characters than remain starting at the
  13.     start position, Str2 will contain just that remainder of Str1. }
  14. InLine(
  15.   $55/          {       push   bp         }
  16.   $89/$E5/      {       mov    bp,sp      }
  17.   $C5/$76/$0C/  {       lds    si,[bp+0C] }
  18.   $FC/          {       cld               }
  19.   $C4/$7E/$04/  {       les    di,[bp+04] }
  20.   $8A/$24/      {       mov    ah,[si]    }
  21.   $20/$E4/      {       and    ah,ah      }
  22.   $74/$16/      {       je     @null      }
  23.   $8A/$5E/$0A/  {       mov    bl,[bp+0A] }
  24.   $38/$DC/      {       cmp    ah,bl      }
  25.   $72/$0F/      {       jb     @null      }
  26.   $8A/$46/$08/  {       mov    al,[bp+08] }
  27.   $88/$C6/      {       mov    dh,al      }
  28.   $00/$DE/      {       add    dh,bl      }
  29.   $FE/$CE/      {       dec    dh         }
  30.   $38/$F4/      {       cmp    ah,dh      }
  31.   $72/$06/      {       jb     @rest      }
  32.   $EB/$0A/      {       jmp    @copy      }
  33.                 { @null:                  }
  34.   $B0/$00/      {       mov    al,00      }
  35.   $EB/$15/      {       jmp    @done      }
  36.                 { @rest:                  }
  37.   $28/$DC/      {       sub    ah,bl      }
  38.   $FE/$C4/      {       inc    ah         }
  39.   $88/$E0/      {       mov    al,ah      }
  40.                 { @copy:                  }
  41.   $88/$C1/      {       mov    cl,al      }
  42.   $30/$ED/      {       xor    ch,ch      }
  43.   $30/$FF/      {       xor    bh,bh      }
  44.   $01/$DE/      {       add    si,bx      }
  45.   $89/$FA/      {       mov    dx,di      }
  46.   $47/          {       inc    di         }
  47.   $F3/$A4/      {   rep movsb             }
  48.   $89/$D7/      {       mov    di,dx      }
  49.                 { @done:                  }
  50.   $88/$05/      {       mov    [di],al    }
  51.                 { @exit:                  }
  52.   $5D           {       pop    bp         }
  53. ) { CopySubStr };
  54.  
  55. procedure StrCopy( var Str1, Str2: string );
  56.   { copy str1 to str2 }
  57. InLine(
  58.   $89/$EA/      {       mov    dx,bp      }
  59.   $89/$E5/      {       mov    bp,sp      }
  60.   $C5/$76/$08/  {       lds    si,[bp+08] }
  61.   $FC/          {       cld               }
  62.   $C4/$7E/$04/  {       les    di,[bp+04] }
  63.   $30/$ED/      {       xor    ch,ch      }
  64.   $8A/$0C/      {       mov    cl,[si]    }
  65.   $41/          {       inc    cx         }
  66.   $F3/$A4/      {   rep movsb             }
  67.   $89/$D5       {       mov    bp,dx      }
  68. ) { StrCopy };
  69.  
  70. function StrPos( var str1, str2: string ): byte;
  71.   { returns position of the first occurrence of str1 in str2 }
  72.   { return value in AX }
  73.   { str1 - string to search for }
  74.   { str2 - string to search in  }
  75. InLine(
  76.   $55/          {       push   bp         }
  77.   $89/$E5/      {       mov    bp,sp      }
  78.   $FC/          {       cld               }
  79.   $C4/$7E/$04/  {       les    di,[bp+04] }
  80.   $30/$ED/      {       xor    ch,ch      }
  81.   $8A/$0D/      {       mov    cl,[di]    }
  82.   $21/$C9/      {       and    cx,cx      }
  83.   $74/$2A/      {       je     @negatief  }
  84.   $47/          {       inc    di         }
  85.   $C5/$76/$08/  {       lds    si,[bp+08] }
  86.   $AC/          {       lodsb             }
  87.   $20/$C0/      {       and    al,al      }
  88.   $74/$21/      {       je     @negatief  }
  89.   $88/$C4/      {       mov    ah,al      }
  90.   $FE/$CC/      {       dec    ah         }
  91.   $AC/          {       lodsb             }
  92.                 { @start:                 }
  93.   $F2/$AE/      { repnz scasb             }
  94.   $75/$18/      {       jne    @negatief  }
  95.   $38/$E1/      {       cmp    cl,ah      }
  96.   $72/$14/      {       jb     @negatief  }
  97.   $89/$F2/      {       mov    dx,si      }
  98.   $89/$CB/      {       mov    bx,cx      }
  99.   $88/$E1/      {       mov    cl,ah      }
  100.   $F3/$A6/      {   rep cmpsb             }
  101.   $74/$0E/      {       je     @positief  }
  102.   $29/$D6/      {       sub    si,dx      }
  103.   $29/$F7/      {       sub    di,si      }
  104.   $89/$D6/      {       mov    si,dx      }
  105.   $89/$D9/      {       mov    cx,bx      }
  106.   $EB/$E4/      {       jmp    @start     }
  107.                 { @Negatief:              }
  108.   $31/$C0/      {       xor    ax,ax      }
  109.   $EB/$09/      {       jmp    @exit      }
  110.                 { @Positief:              }
  111.   $30/$E4/      {       xor    ah,ah      }
  112.   $C4/$7E/$04/  {       les    di,[bp+04] }
  113.   $8A/$05/      {       mov    al,[di]    }
  114.   $29/$D8/      {       sub    ax,bx      }
  115.                 { @Exit:                  }
  116.   $5D           {       pop    bp         }
  117. ) { StrPos };
  118.  
  119. procedure Trim( var Str: string );
  120.   { remove leading and trailing white space from str }
  121. InLine(         { setup }
  122.   $55/          {       push   bp         }
  123.   $89/$E5/      {       mov    bp,sp      }
  124.   $C5/$76/$04/  {       lds    si,[bp+04] }
  125.   $8C/$D8/      {       mov    ax,ds      }
  126.   $8E/$C0/      {       mov    es,ax      }
  127.   $8A/$04/      {       mov    al,[si]    }
  128.   $20/$C0/      {       and    al,al      }
  129.   $74/$45/      {       je     @exit      }
  130.   $89/$F7/      {       mov    di,si      }
  131.   $88/$C4/      {       mov    ah,al      }
  132.               { remove trailing white space }
  133.   $30/$ED/      {       xor    ch,ch      }
  134.   $88/$E1/      {       mov    cl,ah      }
  135.   $01/$CE/      {       add    si,cx      }
  136.                 { @start1:                }
  137.   $8A/$04/      {       mov    al,[si]    }
  138.   $3C/$20/      {       cmp    al,20      }
  139.   $77/$09/      {       ja     @stop1     }
  140.   $4E/          {       dec    si         }
  141.   $FE/$C9/      {       dec    cl         }
  142.   $20/$C9/      {       and    cl,cl      }
  143.   $74/$02/      {       je     @stop1     }
  144.   $EB/$F1/      {       jmp    @start1    }
  145.                 { @stop1:                 }
  146.   $20/$C9/      {       and    cl,cl      }
  147.   $74/$26/      {       je     @done      }
  148.               { look for leading white space }
  149.   $89/$FE/      {       mov    si,di      }
  150.                 { @start2:                }
  151.   $46/          {       inc    si         }
  152.   $8A/$04/      {       mov    al,[si]    }
  153.   $3C/$20/      {       cmp    al,20      }
  154.   $77/$08/      {       ja     @stop2     }
  155.   $FE/$C9/      {       dec    cl         }
  156.   $20/$C9/      {       and    cl,cl      }
  157.   $74/$02/      {       je     @stop2     }
  158.   $EB/$F1/      {       jmp    @start2    }
  159.                 { @stop2:                 }
  160.   $89/$F2/      {       mov    dx,si      }
  161.   $29/$FA/      {       sub    dx,di      }
  162.   $83/$FA/$01/  {       cmp    dx,0001    }
  163.   $74/$0C/      {       je     @done      }
  164.   $FC/          {       cld               }
  165.   $89/$CB/      {       mov    bx,cx      }
  166.   $89/$FA/      {       mov    dx,di      }
  167.   $47/          {       inc    di         }
  168.   $F3/$A4/      {   rep movsb             }
  169.   $89/$D7/      {       mov    di,dx      }
  170.   $89/$D9/      {       mov    cx,bx      }
  171.                 { @done:                  }
  172.   $88/$0D/      {       mov    [di],cl    }
  173.                 { @exit:                  }
  174.   $5D           {       pop    bp         }
  175. ) { Trim };
  176.  
  177. begin
  178.   s1 := '123456789-123456789-';
  179.   s2 := '';
  180.   CopySubStr( s1, 1, 12, s2 );
  181.   writeln( s2 );
  182.  
  183.   s1 := '123qqwerty';
  184.   s2 := 'qwerty';
  185.   CopySubStr( s1, 1, 12, s2 );
  186.   writeln( s2 );
  187.  
  188.   StrCopy( s1, s2 );
  189.   writeln( s2 );
  190.  
  191.   s1 := '123456789-123456789-';
  192.   s2 := '4567';
  193.   writeln( StrPos( s1, s2 ) );
  194.  
  195.   s1 := '  123qqwerty   ';
  196.   s2 := 'qwerty';
  197.   writeln( StrPos( s1, s2 ) );
  198.  
  199.   Trim( s1 );
  200.   writeln( s2 );
  201. end.
  202.